package com.neu.af.dataFilter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.af.dataFilter.entity.MonitorStatus;
import com.neu.af.dataFilter.entity.Tag;
import com.neu.af.dataFilter.entity.TagRuleAlarm;
import com.neu.af.dataFilter.mapper.MonitorStatusMapper;
import com.neu.af.dataFilter.mapper.RuleMapper;
import com.neu.af.dataFilter.mapper.TagRuleAlarmMapper;
import com.neu.af.dataFilter.service.TagRuleAlarmService;
import com.neu.af.dataFilter.service.TagService;
import com.neu.af.dataFilter.service.piservice.PiRead;
import com.neu.af.util.DBUtil;
import com.neu.af.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("tagService")
public class TagServiceImpl implements TagService {
    @Autowired
    com.neu.af.dataFilter.mapper.TagMapper TagMapper;
    @Autowired
    PiRead piRead;
    @Autowired
    TagRuleAlarmMapper tagRuleAlarmMapper;
    @Autowired
    TagRuleAlarmService tagRuleAlarmService;
    @Autowired
    RuleMapper ruleMapper;
    @Autowired
    MonitorStatusMapper monitorStatusMapper;

    @Override
    public int insert(Tag tag) {
        // TODO: 2021/5/9 piread根据tagName获取描述
        // System.out.println(tag.getTag_name());

        String cmd = "cmd.exe /C C:\\alarm\\Release\\PiUtinity.exe 10.72.128.41 pidemo \"\" 5450 \"tag='"
                + tag.getTag_name() +"'\"";
        System.out.println(DBUtil.cmd(cmd));
        //System.out.println(cmd);
        List<String> strings = DataUtil.readStringDataFromTxt(
                "C:\\alarm\\target.txt"
            //    "C:\\alarm\\AlarmFiltering\\target.txt"
        );
//        String descriptor = piRead.getDescriptorFromPI(
//                "tag='"+
//                tag.getTag_name()+"'"," ");
        String descriptor = strings.get(0);
        // 如果找不到descriptor return -1
        // String descriptor = "大于888";
        //System.out.println("descriptor   "+descriptor);
        tag.setDescriptor(descriptor);
        if(TagMapper.getByDescriptor(descriptor).size()>0) {
            return -1;
        }
        TagRuleAlarm tagRuleAlarm = new TagRuleAlarm();
        tagRuleAlarm.setDescriptor(descriptor);
        tagRuleAlarm.setTag_name(tag.getTag_name());
        tagRuleAlarmService.insertOne(tag, ruleMapper.getByAlarmClass("1"), ruleMapper.getByAlarmClass("2"));
        MonitorStatus monitorStatus = new MonitorStatus();
        monitorStatus.setStatus(1);
        monitorStatus.setDescriptor(tag.getDescriptor());
        monitorStatus.setTag_name(tag.getTag_name());
        monitorStatusMapper.insert(monitorStatus);
        return TagMapper.insert(tag);
    }

    @Override
    public int update(Tag rule) {
        return TagMapper.update(rule);
    }

    @Override
    public int delete(Tag tag) {
        tagRuleAlarmMapper.delete(tagRuleAlarmMapper.getByTagName(tag.getTag_name()).getId());
        monitorStatusMapper.deleteByTag(tag);
        return TagMapper.deleteByTag(tag);
    }

    @Override
    public Tag getById(String id) {
        return TagMapper.getById(id);
    }

    @Override
    public List<Tag> getAll() {
        return TagMapper.getAll();
    }

    @Override
    public List<Tag> getAllByFilter(Map<String, Object> map) {
        return TagMapper.getAllByFilter(map);
    }

    @Override
    public List<String> getAllName(Map<String, Object> map) {
        return TagMapper.getAllName(map);
    }

    @Override
    public PageInfo<Tag> getAllByFilter(Integer pageNum, Integer pageSize) {
        return this.getAllByFilter(pageNum, pageSize, new HashMap());
    }

    @Override
    public PageInfo<Tag> getAllByFilter(Integer pageNum, Integer pageSize, Map<String, Object> map) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Tag> tags = TagMapper.getAllByFilter(map);
        return new PageInfo<>(tags);
    }
}
